package com.joyplus.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joyplus.domain.Column;
import com.joyplus.domain.Table;
import com.joyplus.report.core.Result;
import com.joyplus.report.core.ResultGenerator;
import com.joyplus.properties.DatasourceProperties;
import com.joyplus.properties.GeneratorProperties;
import com.joyplus.service.GeneratorService;
import com.joyplus.utils.CodeGenerateUtils;

@Controller
public class GeneratorController {

    @Autowired
    private CodeGenerateUtils codeGenerateUtils;
    @Autowired
    private GeneratorProperties generatorProperties;
    @Autowired
    private DatasourceProperties datasourceProperties;
    @Autowired
    private GeneratorService generatorService;

    @GetMapping("/")
    public String gotoGenerator(Model model){
        model.addAttribute("package", generatorProperties);
        return "generator";
    }

    @GetMapping("/getDatabases")
    @ResponseBody
    public List getDatabases(){
        List databases = generatorService.getDatabases(datasourceProperties.getDatabaseType());
        return databases;
    }

    @GetMapping("/getTables")
    @ResponseBody
    public List getTables(String schemaName){
        List<Table> tables = generatorService.getTables(datasourceProperties.getDatabaseType(), schemaName);
        return tables;
    }

    @PostMapping(value = "generate")
    @ResponseBody
    public Result generate(@RequestBody GeneratorProperties generatorProperties) throws Exception {
        List<Column> columns = generatorService.getColumns(datasourceProperties.getDatabaseType(), generatorProperties.getSchemaName(), generatorProperties.getTableName());
        codeGenerateUtils.generateDoFile(columns, generatorProperties);
        codeGenerateUtils.generateDtoFile(columns, generatorProperties);
        codeGenerateUtils.generateQoFile(columns, generatorProperties);
        codeGenerateUtils.generateControllerFile(columns, generatorProperties);
        codeGenerateUtils.generateServiceFile(columns, generatorProperties);
        codeGenerateUtils.generateDaoFile(columns, generatorProperties);
        codeGenerateUtils.generateMapperFile(columns, generatorProperties);
        return ResultGenerator.genSuccessResult();
    }

}
